﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.IdentityModel.Claims;
using Microsoft.SharePoint.WebPartPages;
using System.Security.Principal;

namespace Veracruz.Facebook.Sharepoint.Webparts
{
    public class DisplayClaims
        : WebPart
    {
        protected override void OnInit(EventArgs e)
        {
            this.Title = "Display claims issued by Veracruz STS";

            base.OnInit(e);
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (ClaimsPrincipal.Current.Identities.Count > 0 && ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                IClaimsIdentity __claimsIdentity = ClaimsPrincipal.Current.Identities[0];
                ShowName(__claimsIdentity, writer);
                ShowClaimsIdentityAsIIdentity(__claimsIdentity, writer);
                ShowClaimsFromClaimsIdentity(__claimsIdentity, writer);
            }
            else
            {
                Label __noClaims = new Label();
                __noClaims.Text = "No claims to display please signin";
                __noClaims.RenderControl(writer);
            }



            base.Render(writer);
        }

        private void ShowName(IClaimsIdentity claimsIdentity, System.Web.UI.HtmlTextWriter writer)
        {
            Label __welcomeMessage = new Label();
            __welcomeMessage.Text = claimsIdentity.Name;
            __welcomeMessage.RenderControl(writer);
            writer.WriteLine("<br />");
        }
        private void ShowClaimsIdentityAsIIdentity(IIdentity identity, System.Web.UI.HtmlTextWriter writer)
        {
            // create the table and labels
            Table table = new Table();
            table.BorderWidth = 1;
            table.Font.Name = "Franklin Gothic Book";
            table.Font.Size = 10;
            table.CellPadding = 3;
            table.CellSpacing = 3;
            table.BorderColor = System.Drawing.Color.Chocolate;

            Label labelTitle = new Label();
            labelTitle.Text = "Values from IIdentity";
            //this.Controls.Add(labelTitle);
            //this.Controls.Add(table);

            // build a row with the identity data
            TableCell tc1 = new TableCell();
            tc1.Text = "";
            tc1.BorderWidth = 1;

            TableCell tc2 = new TableCell();
            tc2.Text = "IsAuthenticated:" + identity.IsAuthenticated.ToString();
            tc2.BorderWidth = 1;

            TableCell tc3 = new TableCell();
            tc3.Text = "Name:" + identity.Name;
            tc3.BorderWidth = 1;

            TableRow tr = new TableRow();
            tr.Controls.Add(tc1);
            tr.Controls.Add(tc2);
            tr.Controls.Add(tc3);

            // add the row to the table
            table.Controls.Add(tr);

            labelTitle.RenderControl(writer);
            table.RenderControl(writer);
        }
        private void ShowClaimsFromClaimsIdentity(IClaimsIdentity claimsIdentity, System.Web.UI.HtmlTextWriter writer)
        {
            // build table and header row
            Table table = new Table();
            TableHeaderCell thc1 = new TableHeaderCell();
            thc1.Text = "Claim Type";

            TableHeaderCell thc2 = new TableHeaderCell();
            thc2.Text = "Claim Value";

            TableHeaderCell thc3 = new TableHeaderCell();
            thc3.Text = "Value Type";

            TableHeaderCell thc4 = new TableHeaderCell();
            thc4.Text = "Subject Name";

            TableHeaderCell thc5 = new TableHeaderCell();
            thc5.Text = "Issuer Name";

            TableHeaderRow th = new TableHeaderRow();
            th.Controls.Add(thc1);
            th.Controls.Add(thc2);
            th.Controls.Add(thc3);
            th.Controls.Add(thc4);
            th.Controls.Add(thc5);
            th.BorderWidth = 1;
            th.BorderColor = System.Drawing.Color.Chocolate;

            // add a row for each claim
            table.Controls.Add(th);
            foreach (Claim claim in claimsIdentity.Claims)
            {
                // Before showing the claims validate that this is an expected claim
                // If it is not in the expected claims list then don't show it
                WriteClaim(claim, table);
            }

            // add the table to the returned page
            table.BorderWidth = 1;
            table.Font.Name = "Franklin Gothic Book";
            table.Font.Size = 10;
            table.CellPadding = 3;
            table.CellSpacing = 3;
            table.BorderColor = System.Drawing.Color.Chocolate;
            Label labelTitle = new Label();
            labelTitle.Text = "Claims from IClaimsIdentity";
            labelTitle.RenderControl(writer);
            table.RenderControl(writer);

        }
        private void WriteClaim(Claim claim, Table table)
        {
            TableCell tc1 = new TableCell();
            tc1.Text = claim.ClaimType;

            TableCell tc2 = new TableCell();
            tc2.Text = claim.Value;

            TableCell tc3 = new TableCell();
            tc3.Text = claim.ValueType.Substring(claim.ValueType.IndexOf('#') + 1);

            TableCell tc4 = new TableCell();
            if ((claim.Subject) != null && (String.IsNullOrEmpty(claim.Subject.Name)) == false)
            {
                tc4.Text = claim.Subject.Name;
            }
            else
            {
                tc4.Text = "Null";
            }

            TableCell tc5 = new TableCell();
            if ((claim.Issuer) != null && (String.IsNullOrEmpty(claim.Issuer.Name)) == false)
            {
                tc5.Text = claim.Issuer.Name;
            }
            else
            {
                tc5.Text = "Null";
            }

            TableRow tr = new TableRow();

            tr.Controls.Add(tc1);
            tr.Controls.Add(tc2);
            tr.Controls.Add(tc3);
            tr.Controls.Add(tc4);
            tr.Controls.Add(tc5);

            tr.BorderColor = System.Drawing.Color.Chocolate;

            table.Controls.Add(tr);
        }
    }
}
